package cn.com.waajyun.shanbayscan.schedule;

import cn.com.waajyun.shanbayscan.cache.CacheMap;
import cn.com.waajyun.shanbayscan.dto.ArticleDto;
import cn.com.waajyun.shanbayscan.service.ArticleService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Component
public class QueryArticlesPageTask {

    private final Log LOG = LogFactory.getLog(QueryArticlesPageTask.class);

    @Autowired
    private ArticleService articleService;

//    @Autowired
//    private RedisTemplate<String, List<ArticleDto>> redisTemplate;

    @Scheduled(cron = "0 */2 * * * ?")
    public void doTask() throws IOException {
        List<ArticleDto> cachedArticles = articleService.getPageContent(1, null, true);
        LOG.debug("存入帖子数据到缓存，帖子数为：" + cachedArticles.size());
//        redisTemplate.opsForValue().set("cachedArticles", cachedArticles);
        CacheMap.put("cachedArticles", cachedArticles);
        CacheMap.put("updateCacheTime", new Date());
    }

}
